package cn.ds.assets.module.assets.service.project;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.time.LocalDateTime;

import cn.ds.assets.module.assets.controller.admin.project.vo.*;
import cn.ds.assets.module.assets.dal.dataobject.project.BaseProjectDO;
import cn.ds.assets.framework.common.pojo.PageResult;

import cn.ds.assets.module.assets.convert.project.BaseProjectConvert;
import cn.ds.assets.module.assets.dal.mysql.project.BaseProjectMapper;

import static cn.ds.assets.framework.common.exception.util.ServiceExceptionUtil.exception;
import cn.ds.assets.framework.common.exception.enums.GlobalErrorCodeConstants;
//import static cn.ds.assets.module.assets.enums.ErrorCodeConstants.*;

/**
 * 项目 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class BaseProjectServiceImpl implements BaseProjectService {

    @Resource
    private BaseProjectMapper baseProjectMapper;

    @Override
    public Long createBaseProject(BaseProjectCreateReqVO createReqVO) {
        // 插入
        BaseProjectDO baseProject = BaseProjectConvert.INSTANCE.convert(createReqVO);
        baseProjectMapper.insert(baseProject);
        // 返回
        return baseProject.getId();
    }

    @Override
    public void updateBaseProject(BaseProjectUpdateReqVO updateReqVO) {
        // 校验存在
        validateBaseProjectExists(updateReqVO.getId());
        // 更新
        BaseProjectDO updateObj = BaseProjectConvert.INSTANCE.convert(updateReqVO);
        baseProjectMapper.updateById(updateObj);
    }

    @Override
    public void deleteBaseProject(Long id) {
        // 校验是否存在
        validateBaseProjectExists(id);
        // 逻辑删除
        baseProjectMapper.updateToDelete(id, LocalDateTime.now());
    }

    private void validateBaseProjectExists(Long id) {
        if (id == null) {
            throw exception(GlobalErrorCodeConstants.ID_NOT_FOUND);
        }
        BaseProjectDO dataDo = baseProjectMapper.selectById(id);
        if (dataDo == null) {
            throw exception(GlobalErrorCodeConstants.DATA_NOT_FOUND);
        }
    }

    @Override
    public BaseProjectDO getBaseProject(Long id) {
        return baseProjectMapper.selectById(id);
    }

    @Override
    public List<BaseProjectDO> getBaseProjectList(Collection<Long> ids) {
        return baseProjectMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BaseProjectDO> getBaseProjectPage(BaseProjectPageReqVO pageReqVO) {
        return baseProjectMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BaseProjectDO> getBaseProjectList(BaseProjectExportReqVO exportReqVO) {
        return baseProjectMapper.selectList(exportReqVO);
    }

}
